'''
Created on 15 mai 2009

@author: tmeedendorp
'''


from util.properties      import Properties
import os
from sets import Set
from xml.dom import minidom
from xml.dom.minidom import Document

from manager.bean_store      import *
import ConfigParser


class ConfigurationManager:
    
    def __init__(self):
        self.configurationDirectory = "./resources/"
        
       # file = open(self.configurationDirectory+"config.cfg")
        self.config = ConfigParser.ConfigParser()
        self.config.read(self.configurationDirectory+"config.cfg")
        ignoredWords = self.config.get("Section Global Parameters", "ignored_words")
        if ignoredWords == None:
            self.ignoreWordsSet = None
        else:
            self.ignoreWordsSet = Set(ignoredWords.split())

    def reload(self):
        self.config = ConfigParser.ConfigParser()
        self.config.read(self.configurationDirectory+"config.cfg")
        ignoredWords = self.config.get("Section Global Parameters", "ignored_words")
        if ignoredWords == None:
            self.ignoreWordsSet = None
        else:
            self.ignoreWordsSet = Set(ignoredWords.split())
            
    def isWordIgnored(self, word):
        if word in self.ignoreWordsSet:
            return True
        return False
    
    def getConfigDir(self):
        return self.configurationDirectory
    
    def getTempDir(self):
        return "/temp/"
    
    def getThematicCategories(self, thematicName):
        return self.config.get("Section Thematic",thematicName.lower()).lower().split(";")
    def getThematicList(self):
        return self.config.get("Section Global Parameters","thematic_list").lower().split(";")
    
    def getCategoryFilters(self, categoryName):
        if categoryName != None and categoryName != "":
            filterList = self.config.get("Section Category", categoryName.lower())
            if filterList != None :
                return filterList.lower().split(";")
        return []
    
    def saveConfiguration(self):
        doc = Document()
        pyminingConfigElt = doc.createElement("pymining-config")
        doc.appendChild(pyminingConfigElt)
        pluginsElt = doc.createElement("plugins")
        pyminingConfigElt.appendChild(pluginsElt)
        for  plugin in getBean("pluginManager").getInstalledPlugins():
            pluginElt = doc.createElement("plugin")
            pluginElt.setAttribute('name', plugin.getName())
            pluginElt.setAttribute('active', str(plugin.getActive()))
            pluginNode = plugin.getXmlConfig();
            if(pluginNode != None):
                pluginElt.appendChild(pluginNode)
            pluginsElt.appendChild(pluginElt)
            
            xmlFile = open(self.getConfigDir()+"config.xml", "w+")
            doc.writexml(xmlFile, "","\t","\n")
            xmlFile.close()
